package com.yc.thread.testvolatile;

import java.util.concurrent.TimeUnit;

/**
 * 如何理解可见性
 */
public class TestVolatile {

    volatile boolean flag = true;

    public void test1() {
//        Thread t1 = new Thread(() -> {
//            flag = true;
//        }, "thread1");

//        Thread t2 = new Thread(() -> {
            System.out.println("开始执行循环");
            while (flag) {
            }
            System.out.println("end 循环");
//        }, "thread2");

//        t1.start();
//        t2.start();

    }

    public static void main(String[] args) {
        TestVolatile testVolatile = new TestVolatile();
//        testVolatile.test1();

        new Thread(testVolatile :: test1, "test1").start();

        try{
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        testVolatile.flag = false;
    }

}
